
import { Component, Injector } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { BasePage } from '../../basePage';
import { nailerService } from '../../../providers/nailerService';
import { StorageService } from '../../../providers/StorageService';
import { PIC_URL } from "./../../../providers/Config";
declare var $: any;

/**
 * Generated class for the MypicturePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-mypicture',
  templateUrl: 'mypicture.html',
})
export class MypicturePage extends BasePage {
  picrooturl = "";//图片服务地址

  userinfo={};
  myPiclist=[];
  activation = false;  //激活图片删除
  flag1:boolean;
  flag2:boolean;
  search:boolean;
  pop=1;  //分页
  selectallflag=false; //全选全不选
  haveData:boolean;     //内容显示器
    searchObj={
      user_id:0,
      collect_type:1
    }
  content='';

  param={
    user_id:0,
    del_type:1,
    nail_id:0
  }
  bottom1: boolean = false;    //加载全部底部显示器
  passFlag:boolean =true;     //编辑和删除
  constructor(injector: Injector,public nailerService:nailerService,public stc:StorageService) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo=this.stc.read('userinfo');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MypicturePage');
    this.getArtistNail('');
  }
  dismissCLI() {
    this.viewCtrl.dismiss(1);
  }
  //获取美甲师的美甲款式
  async getArtistNail(infiniteScroll){
    let param = { 'user_id': this.userinfo['user_id'],pop:this.pop};
    let result = await this.nailerService.showArtistManage(param);
    console.log(result);
    if (result.error_code == 1001) {
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else{
      this.pop++;
      this.haveData=true;   //有内容显示
      let resultData = result.data;
      for (let i = 0; i < resultData.length; i++) {
        resultData[i]['select'] = false;
        resultData[i]['selectimg'] = "assets/img/selectno1.png";
      }
      this.myPiclist=this.myPiclist.concat(resultData);
      if(this.myPiclist.length == 0){
        this.bottom1=false;
        this.haveData=false;
      }
      if (infiniteScroll) {
        this.bottom1=false;
        infiniteScroll.complete(); //请求完成数据更新页面
        if (result.data.length < 21) {
          $(".contentWrap ion-infinite-scroll-content").css("display","none");
          this.bottom1 = true;
        }
      }
    }
  }
  //无限滚动
  doInfinite(infiniteScroll) {
    this.bottom1 = false;
    this.getArtistNail(infiniteScroll);
  }

   /**启用删除小图标 */
   activationCLI() {
    this.activation = true;
    this.passFlag=false;
  }

  /**完成启用图标 */
  activationokCLI() {
    this.activation = false;
    this.passFlag=true;
  }
  /**全选 */
  selectall() {
    if(!this.selectallflag){
      if (this.myPiclist.length > 0) {
        for (let i = 0; i < this.myPiclist.length; i++) {
            this.myPiclist[i]["select"] = true;
            this.myPiclist[i]['selectimg'] = "assets/img/selectok.png";
        }
      }
      this.selectallflag=true;
    }else{
      if (this.myPiclist.length > 0) {
        for (let i = 0; i < this.myPiclist.length; i++) {
            this.myPiclist[i]["select"] = false;
            this.myPiclist[i]['selectimg'] = "assets/img/selectno1.png";
        }
      }
      this.selectallflag=false;
    }
  }

  /**选择删除 */
  selectCLI(i) {
    if (this.myPiclist[i]["select"]) {
      this.myPiclist[i]["select"] = false;
      this.myPiclist[i]["selectimg"] = "assets/img/selectno1.png";
      this.selectallflag=false;
    } else {
      this.myPiclist[i]["select"] = true;
      this.myPiclist[i]["selectimg"] = "assets/img/selectok.png";
    }
  }

  

  /**删除店铺秀 批量选择删除 */
  async delallNailAPI() {
    let count=0;    //个数
    let index=[];       //索引
    let nail_id=[];   //id
    for (let i = 0; i < this.myPiclist.length; i++) {
          if (this.myPiclist[i]['select']) {
            count++;
            index.push(i);
            nail_id.push(this.myPiclist[i]['nail_id']);
          }
    }
    if(count == 0){
      let toast = this.toastCtrl.create({
        message: '请选择美图',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else{
      let alert = this.alertCtrl.create({
        title: '确认删除'+count+'款美图吗？',
        message: '删除后不可恢复！',
        buttons: [
          {
            text: '取消',
            role: 'cancel',
            handler: () => {
              console.log('Cancel clicked');
            }
          },
          {
            text: '删除',
            handler: () => {
              this.delServiceOrNailAPI(count,index,nail_id);
            }
          }
        ]
      });
      alert.present();
    }
  }

  /*未审核通过的单个删除
  * @param j 索引
  */
 deletOne(j){
  let alert = this.alertCtrl.create({
    title: '确认删除该款美图吗？',
    message: '删除后不可恢复哦！',
    buttons: [
      {
        text: '取消',
        role: 'cancel',
        handler: () => {
          console.log('Cancel clicked');
        }
      },
      {
        text: '删除',
        handler: () => {
          this.delServiceOrNailAPI('',j,'');
        }
      }
    ]
  });
  alert.present();
}

  /**删除具体的逻辑代码 */
  async delServiceOrNailAPI(count,index,nail_id){
    console.log(index)
    this.param.user_id=this.userinfo['user_id'];
    this.param.nail_id=nail_id;
    let result = await this.nailerService.delServiceOrNailAPI(this.param);
    console.log(result)
    //成功
    if(result.error_code ==0){
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
      this.loopDel(index);
      //this.selectallflag=false;
    }else if(result.error_code ==1002){
     let toast = this.toastCtrl.create({
        message: result.reason,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  } 

  //循环删除
  loopDel(index){
    for(let i=0;i<index.length;i++){
      if(i==0){
        this.myPiclist.splice(index[i],1);
      }else{
        this.myPiclist.splice(index[i]-i,1);
      }
    }
    
  }
  searchby(){
    this.search =true;
  }
  //搜索美图美款
  async searchAPI(){
    this.myPiclist=[]
    let param={
      user_id:this.userinfo['user_id'],
      search_key:this.content,
      type:2
    }
    let result=await this.nailerService.searchCollectionAPI(param);
    console.log(result);
    if(result.code==1){
      this.myPiclist=result.data;
    }
  }
  //失去焦点恢复
  back(){
    if(this.content==undefined||this.content==''){
      this.search=false;
      this.again();
    }
    console.log(this.content);
  }  
  //搜索
  async sousuo(){
    let param={
      user_id:this.userinfo['user_id'],
      search_key:this.content,
      type:2
    }
    let result=await this.nailerService.searchCollectionAPI(param);
    console.log(result);
    if(result.code==1){
      this.myPiclist=result.data;
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }

  //点击添加美图
  gotoPic(){
    this.navCtrl.push("AddImgPage");
  }

 
  //跳转到美图详情
  gotoNail(i){
    let modal = this.modalCtrl.create("ImgInfoPage", {
      id: i.nail_id
    });
    modal.present();
  }
  //初始化
  again(){
    this.myPiclist=[];
    this.pop=1;
    this.bottom1=false;
    $(".contentWrap ion-infinite-scroll-content").css("display","block");
    this.getArtistNail('')
  }
  
}
